﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;

namespace AlgoImpl
{
    [Serializable]
    public enum eStepThatGotShift
    {
        NONE,
        FIRST,
        SECOND,
        THIRD
    }
    
    [Serializable]
    public class FinalResultHolder
    {
        private Dictionary<string, List<ShiftResult>> m_FinalResultOfSCModul;

        private static volatile FinalResultHolder s_Instance = null;
        private static object s_LockObj = new Object();

        public static FinalResultHolder Instance
        {
            get
            {
                if (s_Instance == null)
                {
                    lock (s_LockObj)
                    {
                        if (s_Instance == null)
                        {
                            s_Instance = new FinalResultHolder();
                        }
                    }
                }

                return s_Instance;
            }
        }

        public void Clear()
        {
            m_FinalResultOfSCModul.Clear();
            createEmptyResultDic();
        }

        private FinalResultHolder() 
        {
            m_FinalResultOfSCModul = new Dictionary<string, List<ShiftResult>>();
            createEmptyResultDic();
        }

        public List<ShiftResult> GetShiftResultOfWorker(string workerId)
        {
            return m_FinalResultOfSCModul[workerId];
        }

        public string GetStepThatWorkerGotShift(string workerId, string Id)
        {
            string step = string.Empty;
            if(IsShiftSetForWorker(workerId, Id))
            {
                foreach (ShiftResult ss in m_FinalResultOfSCModul[workerId])
                {
                    if (ss.Id.Equals(Id))
                    {
                        step = ss.Step.ToString();
                        break;
                    }
                }
            }
            return step;
        }

        public bool IsShiftSetForWorker(string workerId, string Id)
        {
            foreach (ShiftResult ss in m_FinalResultOfSCModul[workerId])
            {
                if (ss.Id.Equals(Id))
                {
                    return true;
                }
            }
            return false;
        }

        public void AddShiftResultToWorkerList(string workerId, ShiftResult sr)
        {       
            m_FinalResultOfSCModul[workerId].Add(sr);
            Logger.Instance.WriteToLog(string.Format("Added Shift {0} on {1} step to worker {2}", sr.Id, sr.Step, workerId)); 
        }

        public void Print()
        {
            foreach (KeyValuePair<string, List<ShiftResult>> kvp in m_FinalResultOfSCModul)
            {

                Console.WriteLine("{0}   {1} : ", kvp.Key, SharedData.Instance.GetWorkerById(kvp.Key).Name);
                foreach (ShiftResult sr in kvp.Value)
                {
                    Console.WriteLine("{0}   {1} : ", sr.Id, sr.Step);
                }
                Console.WriteLine("\n"); ;
            }
        }

        private void createEmptyResultDic()
        {
            foreach (string workerId in SharedData.Instance.GetAllWorkersIds())
            { 
                List<ShiftResult> sr = new List<ShiftResult>();

                m_FinalResultOfSCModul.Add(workerId, sr); 
            }
        }

        public Dictionary<string, List<ShiftResult>> FinalResultOfSCModul
        {
            get { return m_FinalResultOfSCModul; } 
        }
        
    }

    [Serializable]
    public class ShiftResult
    {
        private string m_Id;
        private eStepThatGotShift m_Step = eStepThatGotShift.NONE;

        public ShiftResult(string id, eStepThatGotShift step)
        {
            m_Id = id;
            m_Step = step;
        }

        public eStepThatGotShift Step { get { return m_Step; } }
        public string Id { get { return m_Id; } }

    }
}
